#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rtc.h"
#include "iwdg.h"
#include "gpio.h"


void Error_Handler1(void)
{
    sj_kprintf("set failed \r\n");
}

sj_int8 sj_gpio_clock_config(struct rcc_apb_periph *apb)
{
    if(apb->apb==1)
    {
        SET_BIT(RCC->APB1ENR, (0x01UL<<apb->apb_offset));
    }
    else if(apb->apb==2)
    {
        SET_BIT(RCC->APB2ENR, (0x01UL<<apb->apb_offset));
    }
    return 0;
}

sj_int8 sj_gpio_init(struct gpio_pin *gpio)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    GPIO_InitStruct.Pin = (0x01<<gpio->pin);
    GPIO_InitStruct.Mode = gpio->mode;
    GPIO_InitStruct.Pull = gpio->pull;
    GPIO_InitStruct.Speed = gpio->speed;
    HAL_GPIO_Init((GPIO_TypeDef*)gpio->base,&GPIO_InitStruct);
    return 0;
}

sj_int8 sj_iwdg_clock_config(struct rcc_lsi_periph *iwdg_lsi)
{
    SET_BIT(RCC->CSR, 0x01 << iwdg_lsi->apb_offset);
    return 0;
}

sj_void sj_iwdg_init(struct peripheral_iwdg *hiwdg1)
{
    IWDG_HandleTypeDef hiwdg;
    hiwdg.Instance = (IWDG_TypeDef *)hiwdg1->instance;
    hiwdg.Init.Prescaler = hiwdg1->prescaler;
    hiwdg.Init.Reload = hiwdg1->reload;
    HAL_IWDG_Init(&hiwdg);
}

sj_void sj_iwdg_feed(struct peripheral_iwdg *hiwdg1)
{
    IWDG_HandleTypeDef hiwdg;
    hiwdg.Instance = (IWDG_TypeDef *)hiwdg1->instance;
    hiwdg.Init.Prescaler = hiwdg1->prescaler;
    hiwdg.Init.Reload = hiwdg1->reload;
    HAL_IWDG_Refresh(&hiwdg);
//    sj_kprintf("-------------------------------------------\r\n");
}


